﻿#include "UToolBarManager.h"
#include "Global.h"
UToolBarManager::UToolBarManager(QToolBar* toolBar, QMenuBar *memuBar, QObject* parent)
	: QObject(parent), m_toolBar(toolBar), m_memuBar(memuBar)
{
	m_commonCount = 3;
	m_toolBar->setMovable(false); // 禁止工具栏拖动
	m_mapMenutext.insert(Device, A2T("设备(E)"));
	m_mapMenutext.insert(Scene, A2T("场景(S)"));
	m_mapMenutext.insert(Carouse, A2T("轮播(C)"));
}

QAction * UToolBarManager::addAction(PageType page, const QString & text, const QIcon & icon)
{
	QAction* action = new QAction(icon, text, this);
	action->setToolTip(text);
	m_actionMap[page].append(action);
	return action;
}

QAction * UToolBarManager::addSeparator(PageType page)
{
	QAction* sep = m_toolBar->addSeparator();
	m_toolBar->removeAction(sep);
	m_actionMap[page].append(sep);
	return sep;
}


void UToolBarManager::setToolBtn(PageType page, int nType)
{
	if (m_enPage == page && nType == m_nType)
	{
		return;
	}
	m_enPage = page;
	m_nType = nType;
	switchTo(page);
	//当前PageType不在m_mapMenutext里面就添加进去
	if (!m_menuMapEx.contains(page))
	{
		for (QAction* action : m_memuBar->actions())
		{
			QMenu* menu = action->menu();
			if (menu->title() == m_mapMenutext[page])
			{
				QList<QAction*>vecMenu;
				for (QAction* subAction : menu->actions())
				{
					if (subAction->text() != "")
					{
						vecMenu.push_back(subAction);
					}
				}
				m_menuMapEx.insert(page, vecMenu);
			}
		}
	}
	if (page == Device)
	{
		setDevice(nType);
	}
	else if (page == Scene)
	{
		setScene(nType);
	}
	else if (page == Carouse)
	{
		setCarouse(nType);
	}
}



void UToolBarManager::switchTo(PageType page)
{
	clearNonCommonButtons();
	for (QAction* act : m_actionMap.value(page)) 
	{
		m_toolBar->addAction(act);
	}
	if (page == Device)
	{
		disableMemu(A2T("设备(E)"), true);
		disableMemu(A2T("场景(S)"), false);
		disableMemu(A2T("轮播(C)"), false);
		QVector<bool>vecBool;
		vecBool.push_back(false);
		vecBool.push_back(false);
		vecBool.push_back(false);
		vecBool.push_back(false);
	}
	else if (page == Scene)
	{
		disableMemu(A2T("设备(E)"), false);
		disableMemu(A2T("场景(S)"), true);
		disableMemu(A2T("轮播(C)"), false);
	}
	else 
	{
		disableMemu(A2T("设备(E)"), false);
		disableMemu(A2T("场景(S)"), false);
		disableMemu(A2T("轮播(C)"), true);
	}
}

void UToolBarManager::clearNonCommonButtons()
{
	QList<QAction*> all = m_toolBar->actions();
	while (all.size() > m_commonCount)
	{
		QAction* act = all.last();
		m_toolBar->removeAction(act);
		all.removeLast();
	}
}

void UToolBarManager::disableMemu(const QString & title, bool enabled)
{
	for (QAction* action : m_memuBar->actions()) {
		QMenu* menu = action->menu();
		if (menu && menu->title() == title) {
			for (QAction* subAction : menu->actions()) {
				subAction->setEnabled(enabled);
			}
			break;
		}
	}
}

void UToolBarManager::setDevice(int nType)
{
	//全部启用
	if (nType == 0)
	{
		for (QAction* act : m_actionMap.value(Device))
		{
			if (act->text() == A2T("添加测点"))
			{
				act->setEnabled(false);
			}
			else
			{
				act->setEnabled(true);
			}
		}
		for (QAction* act : m_menuMapEx.value(Device))
		{
			if (act->text() == A2T("添加测点"))
			{
				act->setEnabled(false);
			}
			else
			{
				act->setEnabled(true);
			}
		}
	}
	//禁用删除
	else if (nType == 1)
	{
		for (QAction* act : m_actionMap.value(Device))
		{		
			if(act->text() == A2T("删除")|| act->text() == A2T("添加测点"))
			{
				act->setEnabled(false);
			}
			else
			{
				act->setEnabled(true);
			}
		}

		for (QAction* act : m_menuMapEx.value(Device))
		{
			if (act->text() == A2T("删除(D)") || act->text() == A2T("添加测点"))
			{
				act->setEnabled(false);
			}
			else
			{
				act->setEnabled(true);
			}
		}
	}
	//全部禁用
	else
	{
		for (QAction* act : m_actionMap.value(Device))
		{
			if (act->text() == A2T("删除") || act->text() == A2T("添加节点") || act->text() == A2T("添加设备") || act->text() == A2T("添加测点"))
			{
				act->setEnabled(false);
			}
			else
			{
				act->setEnabled(true);
			}
		}
		for (QAction* act : m_menuMapEx.value(Device))
		{
			act->setEnabled(false);
		}
	}
}

void UToolBarManager::setScene(int nType)
{
	//全部启用
	if (nType == 0)
	{
		for (QAction* act : m_actionMap.value(Scene))act->setEnabled(true);
		for (QAction* act : m_menuMapEx.value(Scene))act->setEnabled(true);
	}
	//禁用删除
	else if (nType == 1)
	{
		for (QAction* act : m_actionMap.value(Scene))
		{
			if (act->text() == A2T("删除"))
			{
				act->setEnabled(false);
			}
			else act->setEnabled(true);
		}

		for (QAction* act : m_menuMapEx.value(Scene))
		{
			if (act->text() == A2T("删除(D)"))
			{
				act->setEnabled(false);
			}
			else act->setEnabled(true);
		}
	}
	//全部禁用
	else
	{
		for (QAction* act : m_actionMap.value(Scene))act->setEnabled(false);
		for (QAction* act : m_menuMapEx.value(Scene))act->setEnabled(false);
	}
}

void UToolBarManager::setCarouse(int nType)
{
	//根节点
	if (nType == 1)
	{
		for (QAction* act : m_actionMap.value(Carouse))
		{
			if (act->text() == A2T("添加节点")|| act->text() == A2T("开始轮播"))
			{
				act->setEnabled(true);
			}
			else
			{
				act->setEnabled(false);
			}		
		}
		for (QAction* act : m_menuMapEx.value(Carouse))
		{
			if (act->text() == A2T("添加节点(N)..."))
			{
				act->setEnabled(true);

			}
			else
			{
				act->setEnabled(false);
			}
		}
		
	}
	//其他
	else if (nType == 0)
	{
		for (QAction* act : m_actionMap.value(Carouse))
		{
			if (act->text() == A2T("添加节点"))act->setEnabled(false);
			else act->setEnabled(true);
		}

		for (QAction* act : m_menuMapEx.value(Carouse))
		{
			if (act->text() == A2T("添加节点(N)..."))act->setEnabled(false);
			else act->setEnabled(true);
		}
	}
	//全部禁用
	else
	{
		for (QAction* act : m_actionMap.value(Carouse))
		{
			if (act->text() == A2T("开始轮播"))act->setEnabled(true);
			else act->setEnabled(false);
		}
		for (QAction* act : m_menuMapEx.value(Carouse))
		{
			if (act->text() == A2T("开始轮播(R)"))act->setEnabled(true);
			else act->setEnabled(false);
		}
	}
}

